package help

import (
	"math"
)

/*
分页处理
*/
func Page(page, pagenum, nums int) map[string]interface{} {
	var firstpage int //前一页地址
	var lastpage int  //后一页地址
	//根据nums总数和pagenum每页数量，计算出来总页数
	totalpage := int(math.Ceil(float64(nums) / float64(pagenum)))
	if totalpage == 0 {
		totalpage = 1
	}
	if page > totalpage {
		page = totalpage
	}
	if page < 1 {
		page = 1
	}
	var pages []int
	switch {
	case page > totalpage-5 && totalpage > 5: //最后5页
		start := totalpage - 5 + 1
		firstpage = page - 1
		lastpage = int(math.Min(float64(totalpage), float64(page+1)))
		pages = make([]int, 5)
		for i, _ := range pages {
			pages[i] = start + i
		}
	case page >= 3 && totalpage >= 5:
		start := page - 3 + 1
		pages = make([]int, 5)
		firstpage = page - 3
		for i, _ := range pages {
			pages[i] = start + i
		}
		firstpage = page - 1
		lastpage = page + 1
	default:
		pages = make([]int, int(math.Min(5, float64(totalpage))))
		for i, _ := range pages {
			pages[i] = i + 1
		}
		firstpage = int(math.Max(float64(1), float64(page-1)))
		lastpage = page + 1
	}
	paginatorMap := make(map[string]interface{})
	paginatorMap["pages"] = pages
	paginatorMap["totalpages"] = totalpage
	paginatorMap["firstpage"] = firstpage
	paginatorMap["lastpage"] = lastpage
	paginatorMap["currpage"] = page
	return paginatorMap
}
